<?php
		
		
	function getToken($name,$password){		
		
		//設定傳送網址
		$url='http://120.96.183.123:5000/v2.0/tokens';
		
		//設定要傳送的json
		$data_string = '{"auth":{"passwordCredentials":{"username": "'.$name.'" , "password":"'.$password.'"}}}';
		
		//開啟cURL連線
		$cURL = curl_init();

		curl_setopt($cURL, CURLOPT_URL, $url);			
		curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($cURL, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Accept: application/json'
		));			
		
		$result = curl_exec($cURL);

		curl_close($cURL);
							
		//print_r($result);

		//將回傳的token儲存
		$token = json_decode($result,true);
		if (isset($token['access']['token']['id']))
		{
			return $token['access']['token']['id'];	
		}
		else
		{
			return "failed";
		}
	}
	


	function getTenants($token){
	
		$url='http://120.96.183.123:5000/v2.0/tenants';
		
		$cURL = curl_init();

		curl_setopt($cURL, CURLOPT_URL, $url);			
		curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "GET");
		//curl_setopt($cURL, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($cURL, CURLOPT_HTTPHEADER,array('X-Auth-Token:'.$token));			
		
		$result = curl_exec($cURL);

		curl_close($cURL);
		
		//print_r($result);
		//echo '</br>';
		
		//將回傳資料轉為陣列儲存
		$tenants = json_decode($result,true);
		
		return $tenants['tenants'];
	}
	
	
	function getTenantstoken($tenantName,$token){
		
		$url='http://120.96.183.123:5000/v2.0/tokens';
		
		//設定要傳送的json
		$data_string = '{"auth":{"tenantName": "'.$tenantName.'","token": {"id": "'.$token.'"}}}';
		
		//開啟cURL連線
		$cURL = curl_init();

		curl_setopt($cURL, CURLOPT_URL, $url);			
		curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($cURL, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Accept: application/json'
		));			
		
		$result = curl_exec($cURL);

		curl_close($cURL);
		
		$Tenantstoken = json_decode($result,true);
		
		return $Tenantstoken['access']['token']['id'];
		
	}
	
	function getCompute_limits($tenantId,$token){
		
		$url='http://120.96.183.123:8774/v2/'.$tenantId.'/limits';
		
		//設定要傳送的json
		//$data_string = '{"auth":{"tenantName": "'.$tenantName.'","token": {"id": "'.$token.'"}}}';
		
		//開啟cURL連線
		$cURL = curl_init();

		curl_setopt($cURL, CURLOPT_URL, $url);			
		curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "GET");
		//curl_setopt($cURL, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Accept: application/json',
			'X-Auth-Token:'.$token
		));			
		
		$result = curl_exec($cURL);

		curl_close($cURL);
		
		$compute_limits = json_decode($result,true);
		
		return $compute_limits['limits']['absolute'];
	
	}
	
	
	function getBlockstorage_limits($tenantId,$token){
		
		$url='http://120.96.183.123:8776/v2/'.$tenantId.'/limits';
		
		//設定要傳送的json
		//$data_string = '{"auth":{"tenantName": "'.$tenantName.'","token": {"id": "'.$token.'"}}}';
		
		//開啟cURL連線
		$cURL = curl_init();

		curl_setopt($cURL, CURLOPT_URL, $url);			
		curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "GET");
		//curl_setopt($cURL, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Accept: application/json',
			'X-Auth-Token:'.$token
		));			
		
		$result = curl_exec($cURL);

		curl_close($cURL);
		
		$blockstorage_limits = json_decode($result,true);
		
		return $blockstorage_limits['limits']['absolute'];
	
	}
	
	
?>